//
//  ByUIOrderDetailRefundVC.m
//  itboye_shop_ios
//
//  Created by mc on 17/2/11.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIOrderDetailRefundVC.h"
//Other
#import "ZKBombView.h"

//View
#import "ByViewTextViewPlaceholder.h"

//Cell
#import "ByCellOrderDetailAddPhoto.h"
@interface ByUIOrderDetailRefundVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray     *sectionTitleAry;
@property (nonatomic, strong) ZKBombView  *reasonView;

@end

@implementation ByUIOrderDetailRefundVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"退款";
    [self initTableView];
    [self createCommitButton];
    [self addLeftBackBarButtonItem];
}

#pragma mark ---getter
-(NSArray *)sectionTitleAry{
    if (!_sectionTitleAry) {
        _sectionTitleAry = @[@"退款原因﹡",@"退款金额﹡",@"退款说明（可不填）",@"上传凭证"];
    }
    return _sectionTitleAry;
}

-(ZKBombView *)reasonView{
    if (_reasonView == nil) {
        _reasonView = [[ZKBombView alloc] init];
        _reasonView.positionMode = ZKBombPositionModeBottom;
        _reasonView.contentMode = ZKBombContentModePicker;
        _reasonView.popMode = ZKBombPopModeBottom;
        _reasonView.dataItems = @[@"我不想买了",@"信息填写错误，重新拍",@"卖家缺货",@"同城见面交易",@"其他原因"];
    }
    return _reasonView;
}

#pragma mark ---创建视图
-(void)initTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    _tableView.delegate = self;
    _tableView.dataSource = self;
}

//创建提交按钮
- (void)createCommitButton{
    UIButton *commitButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.tableView.bottom - 40, SCREEN_WIDTH, 40)];
    [commitButton setTitle:@"提交申请" forState:UIControlStateNormal];
    [commitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    commitButton.backgroundColor = [UIColor redColor];
    commitButton.titleLabel.font = B_FONT(14);
    [self.view insertSubview:commitButton aboveSubview:self.tableView];
    [commitButton addTarget:self action:@selector(commitAction) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark ---UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.sectionTitleAry.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00001;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    headerView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    
    UILabel *sectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, 20)];
    if (section != 1) {
        sectionLabel.text = self.sectionTitleAry[section];
    }else{
        NSString *str = [NSString stringWithFormat:@"%@最多%@",self.sectionTitleAry[section],self.refundPrice];
//        NSDictionary *attriDic = @{NSFontAttributeName:FONT(13),NSForegroundColorAttributeName:[UIColor lightGrayColor]};
//        NSMutableAttributedString *sectionStr = [[NSMutableAttributedString alloc] initWithString:str attributes:attriDic];
//        sectionLabel.attributedText = sectionStr;
        sectionLabel.text = str;
    }
    sectionLabel.font = FONT(14);
    [headerView addSubview:sectionLabel];
    return headerView;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentier = @"refundCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentier];
        cell.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    for (UIView *subview in cell.subviews) {
        [subview removeFromSuperview];
    }
    // 背景图
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.borderColor = [UIColor colorWithWhite:0.90 alpha:1].CGColor;
    bgView.layer.shadowColor = [UIColor colorWithWhite:0.7 alpha:1].CGColor;
    bgView.layer.shadowOffset = CGSizeMake(0, 2);
    bgView.layer.shadowOpacity = 0.5;
    [cell addSubview:bgView];
    
    // 退款原因、退款金额
    NSArray *placeAry = @[@"退款原因",@"请输入退款金额",@"请输入退款说明"];
    if (indexPath.section == 0 || indexPath.section == 1) {
        bgView.frame = CGRectMake(10, 0, SCREEN_WIDTH - 20, 40);
        UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(20, 5, bgView.width - 20, 30)];
        tf.placeholder = placeAry[indexPath.section];
        tf.font = FONT(14);
        tf.rightViewMode = UITextFieldViewModeAlways;
        [cell addSubview:tf];
        
        // 退款原因
        if (indexPath.section == 0) {
            UIImageView *rightImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
            rightImg.backgroundColor = [UIColor redColor];
            tf.rightView = rightImg;
            tf.enabled = NO;
            
            UITapGestureRecognizer *refundTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refundAction)];
            [bgView addGestureRecognizer:refundTap];
            bgView.userInteractionEnabled = YES;

        }
        return cell;

    }
    // 退款说明
    else if(indexPath.section == 2){
        bgView.frame = CGRectMake(10, 0, SCREEN_WIDTH - 20, 80);
        ByViewTextViewPlaceholder *textView = [[ByViewTextViewPlaceholder alloc] initWithFrame:CGRectMake(15, 5, bgView.width - 10, 60)];
        textView.font = FONT(14);
        textView.placeholder = placeAry[indexPath.section];
        [cell addSubview:textView];
        return cell;

    }
    
    // 上传凭证
    else{
        ByCellOrderDetailAddPhoto *photo_cell = [ByCellOrderDetailAddPhoto cellWithTableView:tableView];
        photo_cell.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
        photo_cell.addPhotoBlock = ^(NSMutableArray *imgAry){
            NSLog(@"%@",imgAry);
        };
        return photo_cell;
    
    }
    

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 || indexPath.section == 1) {
        return 40;
    }
    else if(indexPath.section == 2){
        return 80;
    }
    else
        return (SCREEN_WIDTH - 20 - 10)/3 + 20;
}

#pragma mark ---相关点击事件
// 退款原因
- (void)refundAction{
    [self.reasonView show];
}

// 提交申请
- (void)commitAction{
    [ByCoreToolAlert showOnlyText:@"提交申请"];
}

@end
